//
//  controlCenterWorkVC.m
//  Recycling
//
//  Created by jzd on 2018/6/1.
//  Copyright © 2018年 宋佳. All rights reserved.
//

#import "controlCenterWorkVC.h"
#import "ControlCenterTopView.h"
#import "transitionAnnimater.h"
#import "selectDateVC.h"
#import "orderManagerCell.h"

#import "AllOrderVC.h"

#import "selectAreaTabVC.h"
#import "areaModel.h"

@interface controlCenterWorkVC ()<tableViewHeaderDelegate>
@property (strong,nonatomic) ControlCenterTopView *header;
@property (strong,nonatomic) NSMutableArray *dataArray;
@property(strong,nonatomic)transitionAnnimater *transitionVC;
@property (strong,nonatomic)areaModel *areaM;

@property (strong,nonatomic)NSMutableArray *laiyuanArray;
@property (strong,nonatomic)NSDictionary *selectLaiYuan;
@end

@implementation controlCenterWorkVC

-(NSMutableArray *)laiyuanArray{
    if (!_laiyuanArray) {
        _laiyuanArray = [NSMutableArray array];
    }
    return _laiyuanArray;
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(ControlCenterTopView *)header{
    if (!_header) {
        _header = [ControlCenterTopView getHeaderView];
        _header.frame = CGRectMake(0, 0, SJScreenW, 134);
        _header.delegate = self;
    }
    return _header;
}
-(transitionAnnimater *)transitionVC{
    if (_transitionVC == nil) {
        _transitionVC = [[transitionAnnimater alloc]init];
        _transitionVC.torect = CGRectMake(0, SJScreenH-260, SJScreenW, 260);
    }
    return _transitionVC;
}
-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    self.header.height = 175;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = BGCOLOR;
    self.navigationItem.title = @"工单管理";
    self.tableView.tableHeaderView = self.header;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([orderManagerCell class]) bundle:nil] forCellReuseIdentifier:@"cell"];
    
    //查询按钮被点击
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getData) name:orderManagerCheckNotification object:nil];
    
    [self getData];
    [self getLaiYuan];
}
-(void)getData{
    NSDictionary *dict = @{@"wid":@([UserInfoTool shareUserInfoTool].Info.ID)};
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:dict];
    if (self.header.startBut.titleLabel.text.length != 0) {
        [params setObject:self.header.startBut.titleLabel.text forKey:@"sdate"];
    }else{
        [params setObject:@"" forKey:@"sdate"];
    }
    if (self.header.stopBut.titleLabel.text.length != 0) {
        [params setObject:self.header.stopBut.titleLabel.text forKey:@"edate"];
    }else{
        [params setObject:@"" forKey:@"edate"];
    }
    if (![self.header.areaBut.titleLabel.text isEqualToString:@"请选择"]) {
        [params setObject:self.areaM.code forKey:@"areacode"];
    }else{
        [params setObject:@"" forKey:@"areacode"];
    }
    if (![self.header.selectType.titleLabel.text isEqualToString:@"请选择"]) {
        [params setObject:[self.selectLaiYuan objectForKey:@"code"] forKey:@"ordersrc"];
    }else{
        [params setObject:@"" forKey:@"ordersrc"];
    }
    [[SJNetWorkTool sharedTool]requestWithURLString:getEveryStateCount parameters:params method:GET callBack:^(id responseObject, NSError *error) {
        if (responseObject) {
            int code = [[responseObject objectForKey:@"Code"] intValue];
            NSString *msg = [responseObject objectForKey:@"ErrorMessage"];
            id data = [responseObject objectForKey:@"Data"];
            if (code == 1) {
                if (![data isKindOfClass:[NSNull class]]) {
                    NSArray *arr = [data objectForKey:@"rows"];
                    if (arr.count != 0) {
                        self.dataArray = arr.mutableCopy;
                    }else{
                        [self.navigationController.view showToast:@"没有更多数据" completion:nil];
                        return ;
                    }
                    
                }else{
                    [self.navigationController.view showToast:@"暂无数据" completion:nil];
                }
            }else{
                [self.navigationController.view showToast:msg completion:nil];
            }
        }
        [self.tableView reloadData];
    }];
}

-(void)getLaiYuan{
    [[SJNetWorkTool sharedTool]requestWithURLString:GetComSrc parameters:nil method:GET callBack:^(id responseObject, NSError *error) {
        if (responseObject) {
            int code = [[responseObject objectForKey:@"Code"] intValue];
            id data = [responseObject objectForKey:@"Data"];
            if (code == 1) {
                if (![data isKindOfClass:[NSNull class]]) {
                    NSArray *arr = [data objectForKey:@"rows"];
                    self.laiyuanArray = arr.mutableCopy;
                    
                }
            }
        }
    }];
}
#pragma mark -- tableViewHeaderDelegate
-(void)areaButtonOnClick:(UIButton *)button{
    //区域按钮
    
    selectAreaTabVC *vc = [[selectAreaTabVC alloc]init];
    
    __block __weak controlCenterWorkVC *Copy_self = self;
    vc.resultBlock = ^(areaModel *model) {
        Copy_self.areaM = model;
        [Copy_self.header.areaBut setTitle:model.name forState:UIControlStateNormal];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)startButtonOnClick:(UIButton *)button{
    selectDateVC *vc = [[selectDateVC alloc]init];
    vc.modalPresentationStyle = UIModalPresentationCustom;
    vc.transitioningDelegate  = self.transitionVC;
//    __block __weak controlCenterWorkVC *copy_self = self;
    vc.dateBlock = ^(NSString *date) {
        [button setTitle:date forState:UIControlStateNormal];
    };
    [self presentViewController:vc animated:YES completion:nil];
}
-(void)stopButtonOnClick:(UIButton *)button{
    selectDateVC *vc = [[selectDateVC alloc]init];
    vc.modalPresentationStyle = UIModalPresentationCustom;
    vc.transitioningDelegate  = self.transitionVC;
    //    __block __weak controlCenterWorkVC *copy_self = self;
    vc.dateBlock = ^(NSString *date) {
        [button setTitle:date forState:UIControlStateNormal];
    };
    [self presentViewController:vc animated:YES completion:nil];
}
-(void)selectTypeOnClick:(UIButton *)button{
    SelectPickerBuilder *builder = [SelectPickerBuilder new];
    builder.showMask = YES;
    builder.titleText = @"选择订单来源";
    
    SJLog(@"%@",self.laiyuanArray);
    NSArray *arr = [self.laiyuanArray valueForKeyPath:@"name"];
    
    
    __weak __block controlCenterWorkVC *Copy_self = self;
    [SelectPicker showSinglePickerInView:self.view withBuilder:builder strings:arr confirm:^(NSArray<NSString *> * _Nonnull strings, NSArray<NSNumber *> * _Nonnull indexs) {
        SJLog(@"%@--%@",strings,indexs);
        Copy_self.selectLaiYuan = [Copy_self.laiyuanArray objectAtIndex:indexs.firstObject.integerValue];
        [button setTitle:strings.firstObject forState:UIControlStateNormal];
    } cancel:^{
        nil;
    }];;
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = [self.dataArray objectAtIndex:indexPath.row];
    orderManagerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.titL.text = [dict objectForKey:@"OrderStateName"];
    cell.numL.text = [NSString stringWithFormat:@"%d",[[dict objectForKey:@"OrderCount"] intValue]];
    cell.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0);
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dict = [self.dataArray objectAtIndex:indexPath.row];
    int num = [[dict objectForKey:@"OrderCount"] intValue];
    if (num <= 0) {
        return;
    }else{
        //跳转
        AllOrderVC *vc = [[AllOrderVC alloc]init];
        NSString *area = [self.header.areaBut.titleLabel.text isEqualToString:@"请选择"]?@"":self.areaM.code;
        NSDictionary *dic = @{@"sdate":self.header.startBut.titleLabel.text,@"edate":self.header.stopBut.titleLabel.text,@"areacode":area,@"title":[dict objectForKey:@"OrderStateName"],@"OrderState":[dict objectForKey:@"OrderState"]};
        vc.dict = dic;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
